package com.liseen.loader
{
	import com.liseen.events.CgLoaderEvent;
	
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	public class CgLoader extends EventDispatcher
	{
		/////////////////////////类型////////////////////////////////////////////////////
		public static const TYPE_TEXT:String = "text";
		public static const TYPE_VIDEO:String = "video";
		public static const TYPE_BINARY:String = "binary";
		public static const TYPE_SOUND:String = "sound";
		public static const TYPE_XML:String = "xml";
		public static const TYPE_MOVIECLIP:String = "movieclip";
		public static const TYPE_IMAGE:String = "image";
		////////////////////////拓展名///////////////////////////////////////////////////
		public static var IMAGE_EXTENSIONS:Array = ["jpg", "jpeg", "gif", "png"];
		public static var MOVIECLIP_EXTENSIONS:Array = ["swf"];
		public static var XML_EXTENSIONS:Array = ["xml"];
		public static var AVAILABLE_EXTENSIONS:Array = ["swf", "jpg", "jpeg", "gif", "png", "flv", "mp3", "xml", "txt", "js"];
		public static var TEXT_EXTENSIONS:Array = ["txt", "js", "php", "asp", "py"];
		public static var VIDEO_EXTENSIONS:Array = ["flv", "f4v", "f4p", "mp4"];
		public static var SOUND_EXTENSIONS:Array = ["mp3", "f4a", "f4b"];
		
		public static var TYPE_CLASSES:Object = {image:ImageLoadItem,xml:XMLLoadItem,movieclip:SWFLoadItem,binary:BinaryItem};
		
		private static var _instance:CgLoader;
		private var _isLoading:Dictionary;
		private var _curLoaderCount:int;
		private var _loadingItemDic:Dictionary;
		public function CgLoader(value:Singleton)
		{
			this._isLoading = new Dictionary();
			this._loadingItemDic = new Dictionary(); 
		}
		
		public static function getInstance():CgLoader
		{
			if(_instance==null)
			{
				_instance = new CgLoader(new Singleton());
			}
			
			return _instance;
		}
		/**
		 *加载文件 
		 * @param url			路径		
		 * @param preventCache	是否组织缓存
		 * 
		 */		
		public function loadFile(url:String,preventCache:Boolean=false):void
		{
			if(url==null)
			{
				return;
			}
			
			if(_isLoading[url]==true)
			{
				return;
			}
			_isLoading[url] = true;
			
			var loadingItem:LoadItem = getLoadingItem(url);
			if(loadingItem)
			{
				return;
			}
			
			var fileType:String = guessType(url);
			var specificLoadClass:Class = CgLoader.TYPE_CLASSES[fileType] as Class;
			loadingItem = new specificLoadClass(url,fileType);
			loadingItem.preventCache = preventCache;
			loadingItem.addEventListener(ProgressEvent.PROGRESS,onLoadProgressEvent);
			loadingItem.addEventListener(Event.COMPLETE,onLoadCompleteHandle);
			loadingItem.addEventListener(ErrorEvent.ERROR,onErrorHandle);
			loadingItem.load();
			_loadingItemDic[url] = loadingItem;
			
		}
		
		/**
		 *根据url获取LoadItem
		 * @param url
		 * @return 
		 * 
		 */		
		public function getLoadingItem(url:String):LoadItem
		{
			return this._loadingItemDic[url] as LoadItem;
		}
		
		/**
		 *清空 
		 * 
		 */		
		public function destroy():void
		{	
			for(var key:Object in _loadingItemDic)
			{
				_loadingItemDic[key].removeEventListener(Event.COMPLETE,onLoadCompleteHandle);
				_loadingItemDic[key].removeEventListener(ProgressEvent.PROGRESS,onLoadProgressEvent);
				
				delete _loadingItemDic[key];
			}
			
		}
		
		private function guessType(url:String):String
		{
			var type:String;
			var extensions:String;
			var filter_1:String = (url.indexOf("?")>-1)?url.substring(0,url.indexOf("?")):url;
			var filter_2:String = filter_1.substring(filter_1.lastIndexOf("/"));
			var suffix:String = filter_2.substring((filter_2.lastIndexOf(".") + 1)).toLowerCase();
			if(!Boolean(suffix))
			{
				suffix = CgLoader.TYPE_TEXT;
			}
			
			if(suffix==CgLoader.TYPE_IMAGE||CgLoader.IMAGE_EXTENSIONS.indexOf(suffix)>-1)
			{
				type = CgLoader.TYPE_IMAGE;
			}else if(suffix==CgLoader.TYPE_SOUND||CgLoader.SOUND_EXTENSIONS.indexOf(suffix)>-1)
			{
				type = CgLoader.TYPE_SOUND;
			}else if(suffix==CgLoader.TYPE_VIDEO||CgLoader.VIDEO_EXTENSIONS.indexOf(suffix)>-1)
			{
				type = CgLoader.TYPE_VIDEO;
			}else if(suffix==CgLoader.TYPE_XML||CgLoader.XML_EXTENSIONS.indexOf(suffix)>-1)
			{
				type = CgLoader.TYPE_XML;
			}else if(suffix==CgLoader.TYPE_MOVIECLIP||CgLoader.MOVIECLIP_EXTENSIONS.indexOf(suffix)>-1)
			{
				type = CgLoader.TYPE_MOVIECLIP;
			}else
			{
				type = CgLoader.TYPE_BINARY;
			}
			return type;
		}
		
		private function onLoadProgressEvent(event:ProgressEvent):void
		{
			var targetItem:LoadItem = event.target as LoadItem;
			var cgLoaderEvent:CgLoaderEvent = new CgLoaderEvent(CgLoaderEvent.CG_ITEM_LOADING,targetItem,false,false);
			this.dispatchEvent(cgLoaderEvent);
		}
		
		private function onLoadCompleteHandle(event:Event):void
		{
			var targetItem:LoadItem = event.target as LoadItem;
			var cgLoaderEvent:CgLoaderEvent = new CgLoaderEvent(CgLoaderEvent.CG_ITEM_LOADED,targetItem,false,false);
			this.dispatchEvent(cgLoaderEvent);
		}
		
		private function onErrorHandle(event:ErrorEvent):void
		{
			var cgLoaderEvent:CgLoaderEvent = new CgLoaderEvent(CgLoaderEvent.CG_ITEM_LOADERROR,null,false,false);
			cgLoaderEvent.error = event;
			this.dispatchEvent(cgLoaderEvent);
		}
		
	}
}

class Singleton 
{
	
	
}